home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 21 / Cream of the Crop 21 (Terry Blount) (October 1996).iso / editor / auror300.zip / SIEVE.AML < prev    next >
Text File  |  1996-07-17  |  1KB  |  49 lines

  1. //--------------------------------------------------------------------
  2. // SIEVE.AML
  3. // Sieve of Eratosthenes Demo, (C) 1993-1996 by nuText Systems
  4. //
  5. // This macro uses the sieve of Eratosthenes to list prime numbers from
  6. // 1 to 4001 in a popup menu. This method originated in the 3rd century
  7. // B.C. Sieve demonstrates the use of macro language arrays.
  8. //
  9. // Usage:
  10. //
  11. // Select this macro from the Macro List (on the Macro menu), or run it
  12. // from the macro picklist <shift f12>.
  13. //--------------------------------------------------------------------
  14.  
  15. // compile time macros and function definitions
  16. include bootpath "define.aml"
  17.  
  18. // finds primes up to SIZE*2+1
  19. // (2000 is the maximum size of an array)
  20. constant SIZE = 2000
  21.  
  22. // create result buffer
  23. createbuf
  24. ovltext 2
  25.  
  26. // create and initialize flags array to nulls
  27. flags = array SIZE
  28.  
  29. // tests odd numbers only
  30. for i = 1 to SIZE do
  31.   if not flags [i] then
  32.     prime = i + i + 1
  33.     for j = prime + i to SIZE step prime do
  34.       flags [j] = TRUE
  35.     end
  36.     // add prime number to the menu
  37.     addline prime
  38.   end
  39. end
  40.  
  41. // display primes in a popup menu
  42. prime = popup (getcurrbuf) 'Prime Numbers' 10
  43. destroybuf
  44.  
  45. // if a number is selected, enter it at the cursor
  46. if prime then
  47.   send "write" prime
  48. end
  49.